package com.sz.app.oss.api.common.sup;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.constants.CommCodes;
import com.sz.biz.common.sup.entity.SupplierBD;
import com.sz.biz.common.sup.entity.SupplierContact;
import com.sz.biz.common.sup.service.SupplierService;
import com.sz.biz.common.sup.dto.SupplierDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.BizException;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.service.PrincipalUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Function: SupplierController <br>
 * Author: wentao.chang <br>
 * Date: 2016-11-28 14:20:00
 */
@RestController
@RequestMapping("${api}/sup/suppliers/logistics")
@Api(description = " ", tags = "021-001、供应商管理-物流")
public class SupplierLogisticsController extends BaseController {


    @Autowired
    private SupplierService supplierService;

    @ApiOperation(value = "新建供应商", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({@ApiImplicitParam(name = "supplierDto", value = "提交的供应商信息\r\n "
            + "name,simpleName,code,\r\n" + "website,fax,description\r\n"
            + "country,serviceTypeCodes[],rank\r\n", required = true, dataType = "SupplierDto", paramType = "body")})
    @RequestMapping(value = "", method = RequestMethod.POST)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto saveSupplier(@RequestBody SupplierDto supplierDto) {
        supplierDto.setAudited(true);
        //设置为物流系统的供应商
        supplierDto.setType(CommCodes.SUPPLIER_TYPE_LOGISTICS);
        int userId = PrincipalUtils.getAccountId();
        supplierDto.setCreateUserId(userId);
        supplierDto.setLastUpdateUserId(userId);
        supplierDto.setAuditUserId(userId);
        int sid = supplierService.saveSupplier(supplierDto);
        ResultDto resultDto = getSaveSuccessDto(MessagesCodes.MODULE_SUPPLIER);
        resultDto.setData(sid);
        return resultDto;
    }

    @ApiOperation(value = "修改供应商主信息", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "修改供应商的id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "supplierDto", value = "提交的供应商信息\r\n "
                    + "name,simpleName,code,\r\n" + "website,fax,description\r\n"
                    + "country,serviceTypeCodes[],rank\r\n",
                    required = true, dataType = "SupplierDto", paramType = "body")})
    @RequestMapping(value = "/{sid}/main", method = RequestMethod.PUT)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto updateSupplierMain(@PathVariable int sid, @RequestBody SupplierDto supplierDto) {
        supplierDto.setLastUpdateUserId(PrincipalUtils.getAccountId());
        supplierDto.setType(CommCodes.SUPPLIER_TYPE_LOGISTICS);
        supplierService.updateSupplierMain(sid, supplierDto);
        ResultDto resultDto = getUpdateSuccessDto(MessagesCodes.MODULE_SUPPLIER);
        resultDto.setData(sid);
        return resultDto;
    }

    @ApiOperation(value = "修改供应商业务信息", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "修改供应商的id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "supplierDto", value = "提交的供应商信息\r\n "
                    + "bizScope,mainService\r\n", required = true, dataType = "SupplierDto", paramType = "body")})
    @RequestMapping(value = "/{sid}/biz", method = RequestMethod.PUT)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto updateSupplierBiz(@PathVariable int sid, @RequestBody SupplierDto supplierDto) {
        supplierDto.setType(CommCodes.SUPPLIER_TYPE_LOGISTICS);
        supplierDto.setLastUpdateUserId(PrincipalUtils.getAccountId());
        supplierService.updateSupplierBiz(sid, supplierDto);
        ResultDto resultDto = getUpdateSuccessDto(MessagesCodes.MODULE_SUPPLIER);
        resultDto.setData(sid);
        return resultDto;
    }


    @ApiOperation(value = "修改供应商法人财务信息", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "修改供应商的id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "supplierDto", value = "提交的供应商信息\r\n "
                    + "taxpayerNumber,attachFileIds[],lealPerson,\r\n"
                    + "bankAccount,bankName,address,\r\n"
                    + "companyTel\r\n",
                    required = true, dataType = "SupplierDto", paramType = "body")})
    @RequestMapping(value = "/{sid}/financial", method = RequestMethod.PUT)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto updateSupplierLeal(@PathVariable int sid, @RequestBody SupplierDto supplierDto) {
        supplierDto.setType(CommCodes.SUPPLIER_TYPE_LOGISTICS);
        supplierDto.setLastUpdateUserId(PrincipalUtils.getAccountId());
        supplierService.updateSupplierLeal(sid, supplierDto);
        ResultDto resultDto = getUpdateSuccessDto(MessagesCodes.MODULE_SUPPLIER);
        resultDto.setData(sid);
        return resultDto;
    }

    @ApiOperation(value = "新增供应商联系人信息", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "修改供应商的id", required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "/{sid}/contact", method = RequestMethod.PUT)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto saveSupplierContact(@PathVariable int sid,
                                         @RequestBody @ApiParam SupplierContact contact) {
        supplierService.saveSupplierContact(sid, contact);
        ResultDto resultDto = getUpdateSuccessDto(MessagesCodes.MODULE_SUPPLIER_CONTACT);
        resultDto.setData(sid);
        return resultDto;
    }

    @ApiOperation(value = "修改单个供应商联系人信息", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "修改供应商的id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(
                    name = "cid", value = "修改供应商联系人的id", required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "/{sid}/contacts/{cid}", method = RequestMethod.PUT)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto updateSupplierContact(@PathVariable int sid, @PathVariable int cid,
                                           @RequestBody @ApiParam SupplierContact contact) {
        supplierService.updateSupplierContact(sid, cid, contact);
        ResultDto resultDto = getUpdateSuccessDto(MessagesCodes.MODULE_SUPPLIER_CONTACT);
        resultDto.setData(sid);
        return resultDto;
    }

    @ApiOperation(value = "批量删除供应商联系人", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "供应商的id", required = true, dataType = "int", paramType = "path")
    })
    @RequestMapping(value = "/{sid}/contacts/deletions", method = RequestMethod.POST)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto deleteSupplierContacts(@PathVariable int sid, @RequestBody @ApiParam int[] cids) {
        supplierService.deleteSupplierContacts(sid, cids);
        ResultDto resultDto = getDeleteSuccessDto(MessagesCodes.MODULE_SUPPLIER_CONTACT);
        resultDto.setData(sid);
        return resultDto;
    }

    @ApiOperation(value = "保存单个供应商BD信息", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "修改供应商的id", required = true, dataType = "int", paramType = "path")
    })
    @RequestMapping(value = "/{sid}/bd", method = RequestMethod.POST)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto saveSupplierBd(@PathVariable int sid, @RequestBody @ApiParam SupplierBD bd) {

        supplierService.saveSupplierBd(sid, bd);
        ResultDto resultDto = getSaveSuccessDto(MessagesCodes.MODULE_SUPPLIER_BD);
        resultDto.setData(sid);
        return resultDto;
    }

    @ApiOperation(value = "修改单个供应商BD信息", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "修改供应商的id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(
                    name = "bdid", value = "修改供应商BD的id", required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "/{sid}/bd/{bdid}", method = RequestMethod.PUT)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto updateSupplierBd(@PathVariable int sid, @PathVariable int bdid,
                                      @RequestBody @ApiParam SupplierBD bd) {

        supplierService.updateSupplierBd(sid, bdid, bd);
        ResultDto resultDto = getUpdateSuccessDto(MessagesCodes.MODULE_SUPPLIER_BD);
        resultDto.setData(sid);
        return resultDto;
    }

    @ApiOperation(value = "异步校验供应商BD是否存在", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "供应商的id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(
                    name = "id", value = "修改供应商BD条目的id,新增传空", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(
                    name = "bdid", value = "BD的id", required = true, dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/{sid}/bd/check", method = RequestMethod.GET)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto validSupplierBd(@PathVariable int sid, @RequestParam(required = false, defaultValue = "-1") int id,
                                     @RequestParam int bdid) {

        boolean isValid = supplierService.validSupplierBd(sid, id, bdid);
        ResultDto resultDto = new ResultDto();
        resultDto.setData(isValid);
        return resultDto;
    }

    @ApiOperation(value = "批量删除供应商BD", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "sid", value = "修改供应商的id", required = true, dataType = "int", paramType = "path")
    })
    @RequestMapping(value = "/{sid}/bd/deletions", method = RequestMethod.POST)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto deleteSupplierBds(@PathVariable int sid, @RequestBody @ApiParam int[] bdids) {

        supplierService.deleteSupplierBds(sid, bdids);
        ResultDto resultDto = getDeleteSuccessDto(MessagesCodes.MODULE_SUPPLIER_BD);
        resultDto.setData(sid);
        return resultDto;
    }

    @ApiOperation(value = "按id获取一个供应商", notes = "-")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "供应商的id ",
                    required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "/{sid}", method = RequestMethod.GET)
    ///@RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER})
    public ResultDto findSupplierById(@PathVariable int sid) {
        SupplierDto dto = supplierService.findById(sid);
        ResultDto resultDto = new ResultDto();
        resultDto.setData(dto);
        return resultDto;
    }

    @ApiOperation(value = "获取供应商列表", notes = "获取供应商列表，返回供应商列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceTypeCode", value = "类型id",
                    required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "rank", value = "评分 高级筛选收起时传-1", dataType = "float", paramType = "query"),
            @ApiImplicitParam(name = "greaterthan", value = "是否大于", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页 默认第一页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query")})
    @RequestMapping(value = "/search/{serviceTypeCode}", method = {RequestMethod.GET})
    //@RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER})
    public ResultDto getSuppliers(@PathVariable String serviceTypeCode,
                                  @RequestParam(required = false, defaultValue = "") String q,
                                  @RequestParam(required = false, defaultValue = "false") boolean greaterthan,
                                  @RequestParam(required = false, defaultValue = "-1") float rank,
                                  @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                  @RequestParam(required = false, defaultValue = "10") int pageSize)
            throws BizException {
        return supplierService.findSuppliersByFilterListPage(CommCodes.SUPPLIER_TYPE_LOGISTICS, serviceTypeCode, q, rank, greaterthan, pageIndex, pageSize);
    }

    @ApiOperation(value = "按关键字获取供应商列表", notes = "按关键字获取供应商列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页 默认第一页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query")})
    @RequestMapping(value = "/search", method = {RequestMethod.GET})
    //@RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER})
    public ResultDto searchSuppliers(
            @RequestParam(required = false, defaultValue = "") String q,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            @RequestParam(required = false, defaultValue = "10") int pageSize)
            throws BizException {

        return supplierService.findSuppliersByFilter(q,pageIndex, pageSize,CommCodes.SUPPLIER_TYPE_LOGISTICS);
    }

    /**
     * 批量删除供应商
     *
     * @throws Exception
     */
    @ApiOperation(value = "批量删除供应商", notes = "非物理删除 打标志 <br>权限：" + Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "供应商的ids",
                    required = true, dataType = "int[]", paramType = "body")})
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SUPPLIER_LOGISTICS})
    public ResultDto deleteSuppliers(@RequestBody int[] ids) throws Exception {
        supplierService.deleteSuppliers(ids);
        ResultDto dto = getDeleteSuccessDto(MessagesCodes.MODULE_SUPPLIER);
        return dto;
    }

    //异步校验

    /**
     * 异步校验供应商编码
     */
    @ApiOperation(value = "异步校验供应商编码", notes = "异步校验供应商编码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "供应商编码",
                    required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sid", value = "供应商id 修改时传id 新增时传空",
                    dataType = "int", paramType = "query")})
    @RequestMapping(value = "/checkcode", method = {RequestMethod.GET})
    public ResultDto codeCheck(@RequestParam String code,
                               @RequestParam(required = false, defaultValue = "0") int sid) {
        supplierService.supplierCodeCheck(sid, code);
        return new ResultDto();
    }

    /**
     * 异步校验供应商简称
     */
    @ApiOperation(value = "异步校验供应商简称", notes = "异步校验供应商简称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "simplename", value = "供应商简称",
                    required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sid", value = "供应商id 修改时传id 新增时传空",
                    dataType = "int", paramType = "query")})
    @RequestMapping(value = "/checksimplename", method = {RequestMethod.GET})
    public ResultDto supplierSimpleNameCheck(@RequestParam String simplename,
                                             @RequestParam(required = false, defaultValue = "0") int sid) {
        supplierService.supplierSimpleNameCheck(sid, simplename);
        return new ResultDto();
    }

    /**
     * 异步校验供应商名称
     */
    @ApiOperation(value = "异步校验供应商名称", notes = "异步校验供应商名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "供应商名称",
                    required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sid", value = "供应商id 修改时传id 新增时传空",
                    dataType = "int", paramType = "query")})
    @RequestMapping(value = "/checkname", method = {RequestMethod.GET})
    public ResultDto supplierNameCheck(@RequestParam String name,
                                       @RequestParam(required = false, defaultValue = "0") int sid) {
        supplierService.supplierNameCheck(sid, name);
        return new ResultDto();
    }


    @ApiOperation(value = "下拉框中获取供应商列表接口专用", notes = "下拉框中获取供应商列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", dataType = "int", paramType = "query")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public
    @ResponseBody
    ResultDto findProductListForOrder(@RequestParam(required = false) String q,
                                      @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                      @RequestParam(required = false, defaultValue = "10") int pageSize) {
        q = StringUtils.replaceSqlPattern(q);
        QueryParams params = new QueryParams(pageSize, pageIndex, q, null);
        params.put("q", q);
        ResultDto dto = supplierService.findByFilterForOrder(params, CommCodes.SUPPLIER_TYPE_LOGISTICS);
        return dto;
    }

}
